/**
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in
 * compliance with the License.You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied.  See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.wallet.online.jwt;

import java.util.ArrayList;

/**
 * Bean to represent the cart of items purchased
 *
 * @author pying(peng ying)
 *
 */
public class Cart {

  private String totalPrice;
  private String currencyCode;
  private ArrayList<LineItem> lineItems;

  public Cart(String currency) {
    this.currencyCode = currency;
  }

  public Cart(ArrayList<LineItem> items, String currency) {
    this.lineItems = items;
    this.currencyCode = currency;
    updateTotal();
  }

  public String getTotalPrice() {
    return totalPrice;
  }

  public void setTotalPrice(String totalPrice) {
    this.totalPrice = totalPrice;
  }

  public String getCurrency() {
    return currencyCode;
  }

  public void setCurrency(String currency) {
    this.currencyCode = currency;
  }

  public ArrayList<LineItem> getLineItems() {
    return lineItems;
  }

  public void setLineItems(ArrayList<LineItem> lineItems) {
    this.lineItems = lineItems;
  }

  public void addItem(LineItem item) {
    if (lineItems == null) {
      lineItems = new ArrayList<LineItem>();
    }
    lineItems.add(item);
    updateTotal();
  }

  private void updateTotal() {
    Double total = 0.00;
    for (LineItem item : lineItems) {
      total += Double.valueOf(item.getTotalPrice());
    }
    setTotalPrice(Double.toString(total));
  }

}
